




//
//  C_RecentSkimPlayerViewController.m
//  音悦咖
//
//  Created by Best on 15/7/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_RecentSkimPlayerViewController.h"
#import "L_Singleton.h"
#import "L_footPlaytabBar.h"

@interface C_RecentSkimPlayerViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *listArr;


@end

@implementation C_RecentSkimPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.leftButton addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.rightButton.hidden = YES;
    [self getListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 模态返回
- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

#pragma mark CreateBackImageView

- (void)createblackGroundImageView
{
    blackGroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    [songListCollection addSubview:blackGroundImageView];
    blackGroundImageView.userInteractionEnabled = YES;
    blackGroundImageView.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"recentBlack", @"jpg")];
    [blackGroundImageView release];
}



#pragma mark CreateCollection
- (void)createHeaderCollection
{
    headerLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    headerCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) collectionViewLayout:headerLayout];
    headerLayout.minimumLineSpacing = 0;
    headerCollection.pagingEnabled = YES;
    headerLayout.itemSize = CGSizeMake(SCREEN_WIDTH, headerCollection.frame.size.height);
    headerLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    headerCollection.delegate = self;
    headerCollection.dataSource = self;
    headerCollection.tag = 2000;
    headerCollection.backgroundColor = [UIColor clearColor];
    [blackGroundImageView addSubview:headerCollection];
    
    
#pragma mark Cell重用池
    [headerCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"temp"];
    [headerCollection registerClass:[C_HeaderFirstCollectionViewCell class] forCellWithReuseIdentifier:@"first"];
    [headerCollection registerClass:[C_HeaderSecondCollectionViewCell class] forCellWithReuseIdentifier:@"second"];
    
    
}

#pragma mark GetListData

- (void)getListData
{
    //URL = [NSString stringWithFormat:@"%@%@%@", API_LIST_URL_FOUNT, self.model.listId, API_LIST_URL_BLACK];
    [AFNetworkHandler GETWithAFNByURL:self.URL completion:^(id result) {
        
        self.listArr = [C_MusicPlayerModel arrayWithModelByArray:[result objectForKey:@"data"]];
        [self createListCollection];
        [self createblackGroundImageView];
        [self createHeaderCollection];
        
        
    }];
}

#pragma mark CreateListCollection
- (void)createListCollection
{
    listLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    songListCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:listLayout];
    listLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 6);
    listLayout.minimumLineSpacing = 1;
    songListCollection.tag = 2001;
    songListCollection.delegate = self;
    songListCollection.dataSource = self;
    songListCollection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:songListCollection];
    [songListCollection release];
    
    [songListCollection registerClass:[C_MusicPlayerListCollectionViewCell class] forCellWithReuseIdentifier:@"C_MusicPlayerListCollectionViewCell"];
    
    [songListCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}


#pragma mark CollectionDelegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2000: {
            
            return 3;
            break;
        }
        case 2001: {
            
            return self.listArr.count;
            break;
        }
        default:
            break;
    }
    return 0;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000:{
            switch (indexPath.item) {
                case 0:{
                    
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"temp" forIndexPath:indexPath];
                    cell.backgroundColor = [UIColor clearColor];
                    return cell;
                    
                    break;
                }
                case 1:{
                    C_HeaderFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"first" forIndexPath:indexPath];
                    cell.backgroundColor = RGBA(100, 200, 100, 0.5);
                    cell.nameLabel.text = self.model.title;
                    cell.countLabel.text = [self.model.count description];
                    cell.imageView.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"lloo", @"png")];
                    
                    return cell;
                    break;
                }
                case 2:{
                    C_HeaderSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"second" forIndexPath:indexPath];
                    cell.backgroundColor = RGBA(180, 180, 180, 0.5);
                    cell.desuLabel.text = self.model.details;
                    return cell;
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case 2001: {
            C_MusicPlayerListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_MusicPlayerListCollectionViewCell" forIndexPath:indexPath];
            cell.model = self.listArr[indexPath.item];
            
            return cell;
            break;
        }
            
        default:
            break;
    }
    
    return nil;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2001: {
            // 歌曲播放
            L_Singleton *single = [L_Singleton shareManager];
            L_footPlaytabBar *footTabBar = [L_footPlaytabBar shareTabBar];
            // 传值
            single.song_id = [self.listArr[indexPath.item] song_id];
            single.index = indexPath.item;
            
            
            // 临时列表
            if (single.array == nil) {
                for (PlayerModel *play in self.listArr) {
                    [single.array addObject:play];
                    
                }
                NSString *url = [[[single.array[indexPath.item] url_list] firstObject] objectForKey:@"url"];
                [single playWithUrl:url];
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.item;
                
                
                footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.item] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
                
                
            } else if ([single.array firstObject] != [self.listArr firstObject]) {
                single.array = NULL;
                single.array = [NSMutableArray array];
                for (PlayerModel *play in self.listArr) {
                    [single.array addObject:play];
                    
                }
                
                NSString *url = [[[single.array[indexPath.item] url_list] objectAtIndex:1] objectForKey:@"url"];
                
                [single playWithUrl:url];
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.item;
                footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.item] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            } else {
                single.song_id = [single.array[indexPath.item] song_id];
                NSString *url = [[[single.array[indexPath.item] url_list] firstObject] objectForKey:@"url"];
                [single playWithUrl:url];
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.item;
                footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            }
            break;
        }
        default:
            break;
    }
}
#pragma mark Headr
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2001: {
            UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
            return reusable;
            break;
        }
        default:
            break;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2001:
            return CGSizeMake(0, 200);
            break;
            
        default:
            break;
    }
    return CGSizeMake(0, 0);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
